
// <copyright file="CounterCategoryInfo.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The counter category info.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Diagnostics.Performance.Dtos
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// The counter category info.
    /// </summary>
    [Serializable]
    public class CounterCategoryInfo
    {
        #region Constructors and Finalizers

        /// <summary>
        /// Initializes a new instance of the <see cref="CounterCategoryInfo"/> class.
        /// </summary>
        public CounterCategoryInfo()
        {
            this.Instances = new List<CounterInstanceInfo>();
            this.CounterNames = new List<string>();
        }

        #endregion Constructors and Finalizers

        #region Public Properties

        /// <summary>
        /// Gets or sets the category name.
        /// </summary>
        public string CategoryName { get; set; }

        /// <summary>
        /// Gets or sets the counter names.
        /// </summary>
        public List<string> CounterNames { get; set; }

        /// <summary>
        /// Gets or sets the instances.
        /// </summary>
        public List<CounterInstanceInfo> Instances { get; set; }

        /// <summary>
        /// Gets or sets the reference type counter names.
        /// </summary>
        public List<string> ReferenceTypeCounterNames { get; set; }

        #endregion Public Properties

        #region Public Methods and Operators

        /// <summary>
        /// The get counter value.
        /// </summary>
        /// <param name="instanceName">
        /// The instance name.
        /// </param>
        /// <param name="counterName">
        /// The counter name.
        /// </param>
        /// <param name="ignoreCase">
        /// The ignore case.
        /// </param>
        /// <returns>
        /// The <see cref="double?"/>.
        /// </returns>
        public double? GetCounterValue(string instanceName, string counterName, bool ignoreCase)
        {
            int index = -1;
            for (int i = 0; i < this.CounterNames.Count; i++)
            {
                if (string.Compare(this.CounterNames[i], counterName, ignoreCase) == 0)
                {
                    index = i;
                    break;
                }
            }

            if (index != -1)
            {
                CounterInstanceInfo instance = this.GetInstanceInfo(instanceName, ignoreCase);
                if (instance != null)
                {
                    return instance.CounterValues[index];
                }
            }

            return null;
        }

        /// <summary>
        /// The get instance info.
        /// </summary>
        /// <param name="instanceName">
        /// The instance name.
        /// </param>
        /// <param name="ignoreCase">
        /// The ignore case.
        /// </param>
        /// <returns>
        /// The <see cref="CounterInstanceInfo"/>.
        /// </returns>
        public CounterInstanceInfo GetInstanceInfo(string instanceName, bool ignoreCase)
        {
            foreach (CounterInstanceInfo instance in this.Instances)
            {
                if (string.Compare(instance.InstanceName, instanceName, ignoreCase) == 0)
                {
                    return instance;
                }
            }

            return null;
        }

        #endregion Public Methods and Operators
    }
}